package com.lenovo.smarttraffic.ui.activity;

import android.app.Dialog;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.lenovo.smarttraffic.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

public class Activity_1 extends AppCompatActivity {
    private List<Integer> user_list;
    private List<Boolean> checkbox_list;
    private  MyAdapter myAdapter;
    private TextView chongzhiCarNumber;
    private EditText chongzhiMoney;
    private Button queding;
    private Button quxiao,piliang_btn;

    private ImageView fanhuiBtn;
    private TextView lishiBtn;
    private Button piliangBtn;
    private ListView listview1;
    private SQLiteDatabase db;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_1);
        init();
    }
    private void init(){
        db = new Activity_1_Mysqlite3(Activity_1.this,"user1.db",null,1).getWritableDatabase();
        fanhuiBtn = (ImageView) findViewById(R.id.fanhui_btn);
        fanhuiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        lishiBtn = (TextView) findViewById(R.id.lishi_btn);
        lishiBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Activity_1.this,Activity_1_2.class);
                startActivity(intent);
            }
        });
        piliangBtn = (Button) findViewById(R.id.piliang_btn);
        piliangBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alert(999);
            }
        });
        listview1 = (ListView) findViewById(R.id.listview1);

        View head_view = getLayoutInflater().inflate(R.layout.activity_1_head_view_item,null);
        listview1.addHeaderView(head_view);

        checkbox_list = new ArrayList<>();
        user_list = new ArrayList<>();
            user_list.add(50);
            user_list.add(50);
            user_list.add(50);
            user_list.add(50);
        for (int i=0;i<user_list.size();i++){
            checkbox_list.add(false);
        }
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.hide();
        }
        myAdapter = new MyAdapter();
        listview1.setAdapter(myAdapter);
    }
    private void alert(int method){
        View view = getLayoutInflater().inflate(R.layout.activity_1_alert_item_view,null);
        chongzhiCarNumber = (TextView) view.findViewById(R.id.chongzhi_car_number);
        chongzhiMoney = (EditText) view.findViewById(R.id.chongzhi_money);
        queding = (Button) view.findViewById(R.id.queding);
        quxiao = (Button) view.findViewById(R.id.quxiao);
        if(method==999){
            for (int i=1;i<user_list.size()+1;i++){
                if(checkbox_list.get(i-1)){
                    chongzhiCarNumber.setText(chongzhiCarNumber.getText().toString()+i+",");
                }
            }
        }else {
            chongzhiCarNumber.setText(method+1+"");

        }

        AlertDialog.Builder builder = new AlertDialog.Builder(Activity_1.this);
            builder.setView(view);
        Dialog dialog = builder.show();
        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!chongzhiMoney.getText().toString().trim().isEmpty()){
                    int money_temp = Integer.valueOf(chongzhiMoney.getText().toString().trim());
                    if(money_temp < 1){
                        Toast.makeText(Activity_1.this, "充值金额最少为一元!", Toast.LENGTH_SHORT).show();
                        return;
                    }
                    if(method==999){
                        for (int i=0;i<user_list.size();i++){
                            if(checkbox_list.get(i)){
                                get_pay(i,money_temp);
                                ContentValues values = new ContentValues();
                                    values.put("car_number",i);
                                    values.put("money",money_temp);
                                    values.put("user","admin");
                                    values.put("time",get_now());
                                db.insert("user1",null,values);
                            }
                        }

                    }else {
                        get_pay(method,money_temp);
                        ContentValues values = new ContentValues();
                        values.put("car_number",method);
                        values.put("money",money_temp);
                        values.put("user","admin");
                        values.put("time",get_now());
                        db.insert("user1",null,values);

                    }
                    dialog.dismiss();

                }else {
                    return;
                }
            }
        });


    }
    private String get_now(){
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
        Date date = null;
        Calendar calendar = Calendar.getInstance();
        date = calendar.getTime();
        return simpleDateFormat.format(date);
    }
    private void get_pay(int method,int money_number){
        if(method==999){
//            多选
            for(int i=0;i<user_list.size();i++){
                if(checkbox_list.get(i)){
                    user_list.set(i,user_list.get(i)+money_number);
                }
            }
            Toast.makeText(this, "批量充值成功！", Toast.LENGTH_SHORT).show();
        }else {
//            单选
            user_list.set(method,user_list.get(method)+money_number);
            Toast.makeText(this, "充值成功！", Toast.LENGTH_SHORT).show();
        }
        myAdapter.notifyDataSetChanged();

    }
    class MyAdapter extends BaseAdapter{
        private TextView carNumberText;
        private TextView moneyText;
        private CheckBox checkbox1;
        private Button chongzhiBtn;

        @Override
        public int getCount() {
            return user_list.size();
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View view = getLayoutInflater().inflate(R.layout.activity_1_list_view_item,null);
            carNumberText = (TextView) view.findViewById(R.id.car_number_text);
            moneyText = (TextView) view.findViewById(R.id.money_text);
            checkbox1 = (CheckBox) view.findViewById(R.id.checkbox1);
            chongzhiBtn = (Button) view.findViewById(R.id.chongzhi_btn);
                carNumberText.setText(String.valueOf(position+1));
                moneyText.setText(""+user_list.get(position)+"元");
                checkbox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked){
                            checkbox_list.set(position,true);
                        }else {
                            checkbox_list.set(position,false);
                        }
                    }
                });
            chongzhiBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    alert(position);
                }
            });
            return view;
        }
    }
}
